package com.mindskip.xzs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.dto.admin.user.UserEventLogPageDTO;
import com.mindskip.xzs.domain.entity.UserEventLog;
import com.mindskip.xzs.domain.dto.KeyValue;
import com.mindskip.xzs.domain.vo.admin.user.UserEventLogPageVO;
import com.mindskip.xzs.repository.UserEventLogMapper;
import com.mindskip.xzs.service.UserEventLogService;
import com.mindskip.xzs.utility.BeanCopyUtils;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.PageInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class UserEventLogServiceImpl extends BaseServiceImpl<UserEventLog> implements UserEventLogService {

    private final UserEventLogMapper userEventLogMapper;

    @Autowired
    public UserEventLogServiceImpl(UserEventLogMapper userEventLogMapper) {
        super(userEventLogMapper);
        this.userEventLogMapper = userEventLogMapper;
    }

    // 得到当前月份这一段时间内每天的用户日志表的数量
    @Override
    public List<Integer> selectMonthCount() {
        // 得到当前月份的开始日期
        Date startTime = DateTimeUtil.getMonthStartDay();
        // 得到当前月份的结束日期
        Date endTime = DateTimeUtil.getMonthEndDay();
        // 将表中creat_time字段属于当前月份的所有数据查询出来，存在 KeyValue 里，key 存储着日期(yyyy-MM-dd)，value 存储着该日期的所有日志数量
        List<KeyValue> mouthCount = userEventLogMapper.selectCountByDate(startTime, endTime);

        /*
         * 例如：mouthCount:[{key: 2022-11-01,value: 2},{key: 2022-11-02,value: 1},{key: 2022-11-03,value: 3},{key: 2022-11-06,value: 1},{key: 2022-11-07,value: 1}]
         * 转换为 [2, 1, 3, 0, 0, 1, 1]
         */
        // 得到当前日期距月初的所有日期
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream()
                .map(new Function<String, Integer>() {
                    // 将String类型的转为Integer类型
                    @Override
                    public Integer apply(String md) {
                        KeyValue keyValue = null;
                        for (KeyValue kv : mouthCount) {
                            if (kv.getName().equals(md)) {
                                keyValue = kv;
                                break;
                            }
                        }
                        return null == keyValue ? 0 : keyValue.getValue();
                    }
                })
                .collect(Collectors.toList());
/*        return mothStartToNowFormat.stream()
                .map(md -> {
                    KeyValue keyValue = mouthCount
                            .stream()
                            .filter(kv -> kv.getName().equals(md))
                            .findAny()
                            .orElse(null);
                    return null == keyValue ? 0 : keyValue.getValue();
                })
                .collect(Collectors.toList());*/
    }

    @Override
    public PageInfo<UserEventLogPageVO> getUserEventLogByPage(UserEventLogPageDTO userEventLogPageDTO) {

        PageHelper.startPage(userEventLogPageDTO.getPageIndex(), userEventLogPageDTO.getPageSize(), "id desc");
        List<UserEventLog> userEventLogList = userEventLogMapper.selectByPage(userEventLogPageDTO);
        PageInfo<UserEventLog> oldPageInfo = new PageInfo<>(userEventLogList);

        List<UserEventLogPageVO> userEventLogPageVOList = userEventLogList
                .stream()
                .map(new Function<UserEventLog, UserEventLogPageVO>() {
                    @Override
                    public UserEventLogPageVO apply(UserEventLog userEventLog) {

                        // 给 UserEventLogPageVO 的 id、userId、userName、realName、content 属性赋值
                        UserEventLogPageVO userEventLogPageVO = BeanCopyUtils.copyBean(userEventLog, UserEventLogPageVO.class);

                        // 给 UserEventLogPageVO 的 createTime 属性赋值
                        userEventLogPageVO.setCreateTime(DateTimeUtil.dateFormat(userEventLog.getCreateTime()));
                        return userEventLogPageVO;
                    }
                })
                .collect(Collectors.toList());

        return PageInfoUtil.copyMap(oldPageInfo, userEventLogPageVOList);
    }

    @Override
    public List<UserEventLog> getUserEventLogByUserId(Integer userId) {
        return userEventLogMapper.selectByUserId(userId);
    }

}
